#Author: Luke Keele
#Matching Analysis for Black Mayoral Turnout in LA
#Date: 3/20/2013

#Load Libraries
library(foreign)
library(exactRankTests)
library(ggplot2)

rm(list=ls())

setwd("~/Documents/Black Mayors/Replication File/Data")

vturn <- read.dta("match-all.dta")
vturn$blackf <- factor(vturn$black, labels = c("No African American Candidate", "African-American Candidate"))

ggplot(vturn, aes(x=blackf, y=blackpop_pct2, fill=blackf)) + 
	geom_boxplot(notch=TRUE) +	
	guides(fill=FALSE) + xlab("") + ylab("") +
	scale_fill_brewer(palette = "Paired")

rm(vturn)


###################################################################
# General Election
###################################################################

vturn <- read.dta("match-all.dta")
vturn$treat <- 1 - vturn$black
rank_out <- wilcox.exact(black_turnout ~ as.factor(treat), paired=FALSE, distribution="exact", conf.int=TRUE, alternative="two.sided", data=vturn)
rank_out$p.value
rank_out$conf.int
rank_out$estimate

table(vturn$black)
N <- sum(table(vturn$black))
col1 <- c(rank_out$estimate, rank_out$conf.int, rank_out$p.value, N)
col1

rm(vturn, N, rank_out)

###################################################################
# Runoff 
###################################################################

vturn <- read.dta("match-runoff.dta")
vturn$treat <- 1 - vturn$black
rank_out <- wilcox.exact(black_turnout ~ as.factor(treat), paired=FALSE, distribution="exact", conf.int=TRUE, alternative="two.sided", data=vturn)
rank_out$p.value
rank_out$conf.int
rank_out$estimate

table(vturn$black)
N <- sum(table(vturn$black))
N
col2 <- c(rank_out$estimate, rank_out$conf.int, rank_out$p.value, N)
col2

rm(vturn, N, rank_out)

###################################################################
# Run Gen Elect Race Match
###################################################################

vturn <- read.dta("match-runoff.dta")
vturn <- subset(vturn, black_gen==1)
vturn$treat <- 1 - vturn$black
rank_out <- wilcox.exact(black_turnout ~ as.factor(treat), paired=FALSE, distribution="exact", conf.int=TRUE, alternative="two.sided", data=vturn)
rank_out$p.value
rank_out$conf.int
rank_out$estimate

table(vturn$black)
N <- sum(table(vturn$black))

col3 <- c(rank_out$estimate, rank_out$conf.int, rank_out$p.value, N)
col3


setwd("~/Documents/Black Mayors/Replication File")
source("print_tab.R")

print.table(col1, col2, col3)

